package service

import (
	"context"

	model "gitee.com/hust-ideas/little-genius-backend/model"
	"gorm.io/gorm"
)

func (s *ServiceContext) KeywordsGet(req model.KeywordsGetReq, ctx context.Context) (resp model.KeywordsGetResp, err error) {
	resp = model.KeywordsGetResp{}
	keywords, err := s.Model.KeywordsModel.FindByKUId(req.UserId)
	if err != nil {
		// 如果没有找到关键词，返回空列表
		if err.Error() == gorm.ErrRecordNotFound.Error() {
			return resp, nil
		} else {
			return resp, err
		}
	}
	for _, keyword := range keywords {
		resp.Keywords = append(resp.Keywords, model.Keyword{
			Id:         uint64(keyword.ID),
			Name:       keyword.KName,
			Grade:      keyword.KGrade,
			Subject:    keyword.KSubject,
			Difficulty: keyword.KDifficulty,
		})
	}
	return
}
